package com.rs.game.player.controlers;

import com.rs.cache.loaders.ObjectDefinitions;
import com.rs.game.Animation;
import com.rs.game.Entity;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.npc.NPC;
import com.rs.game.player.ForceMovement;
import com.rs.game.player.Player;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;


public class Wilderness extends Controler {

	private boolean showingSkull;
	
	@Override
	public void start() {
		moved();
	}
	
	@Override
	public boolean login() {
		moved();
		return false;
	}
	
	@Override
	public boolean canAttack(Entity target) {
		if(target instanceof Player) {
			Player p2 = (Player) target;
			if(player.isCanPvp() && !p2.isCanPvp())  {
				player.getPackets().sendGameMessage("That player is not in the wilderness.");
				return false;
			}
			if(canHit(target)) {
				if(target.getAttackedBy() != player)
					player.setSkull();
				return true;
			}
			//warning message here
			return false;
		}
		return true;
	}
	
	public boolean canHit(Entity target) {
		if(target instanceof NPC)
			return true;
		Player p2 = (Player) target;
		if(Math.abs(player.getSkills().getCombatLevel()-p2.getSkills().getCombatLevel()) > getWildLevel()) 
			return false;
		return true;
	}
	
	public boolean processMagicTeleport(WorldTile toTile) {
		if(getWildLevel() > 20) {
			player.getPackets().sendGameMessage("A mysterious force prevents you from teleporting");
			return false;
		}
		removeIcon();
		removeControler();
		return true;
	}
	
	public boolean processItemTeleport(WorldTile toTile) {
		if(getWildLevel() > 30) {
			player.getPackets().sendGameMessage("A mysterious force prevents you from teleporting");
			return false;
		}
		removeIcon();
		removeControler();
		return true;
	}
	
	public void showSkull() {
		player.getInterfaceManager().sendTab(player.getInterfaceManager().hasRezizableScreen() ? 5 : 17, 381);
	}
	
	@Override
	public boolean processObjectClick1(final WorldObject object) {
		if(object.getId() == 1441) {
			player.addStopDelay(4);
			player.setNextAnimation(new Animation(6132));
			final WorldTile toTile = new WorldTile(player.getX(), object.getY()-1, object.getPlane());
			player.setNextForceMovement(new ForceMovement(new WorldTile(player), 1, toTile, 2, 2));
			final ObjectDefinitions objectDef = object.getDefinitions();
			WorldTasksManager.schedule(new WorldTask() {
				@Override
				public void run() {
					player.setNextWorldTile(toTile, false);
					player.setNextFaceWorldTile(new WorldTile(object.getCoordFaceX(objectDef.getSizeX(), objectDef.getSizeY(), object.getRotation())
							, object.getCoordFaceY(objectDef.getSizeX(),objectDef.getSizeY(), object.getRotation())
							, object.getPlane()));
					removeControler();
				}
			}, 2);
			return false;
		}
		return true;
	}
	
	
	@Override
	public void sendInterfaces() {
			if(isAtWild(player))
				showSkull();
	}
	@Override
	public boolean sendDeath() {
		removeIcon();
		removeControler();
		return true; //TODO custom dead without graves
	}
	
	@Override
	public void moved() {
		boolean isAtWild = isAtWild(player);
		boolean isAtWildSafe = isAtWildSafe();
		if(!showingSkull && isAtWild && !isAtWildSafe) {
			showingSkull = true;
			player.setCanPvp(true);
			showSkull();
		}else if (showingSkull && (isAtWildSafe || !isAtWild)) {
			removeIcon();
		}else if (!isAtWild(player) && !isAtWildSafe()) {
			removeIcon();
			removeControler();
		}
	}
	
	public void removeIcon() {
		if(showingSkull) {
			showingSkull = false;
			player.setCanPvp(false);
			player.getPackets().closeInterface(player.getInterfaceManager().hasRezizableScreen() ? 5 : 17);
		}
	}
	
	
	@Override
	public boolean logout() {
		return false; //so doesnt remove script
		
	}
	
	@Override
	public void forceClose() {
		removeIcon();
	}
	
	public static final boolean isAtWild(WorldTile tile) {
		return (tile.getX() >= 2940 && tile.getX() <= 3395 && tile.getY() >= 3524 && tile.getY() <= 4000) || (tile.getX() >= 3264 && tile.getX() <= 3279 && tile.getY() >= 3279 && tile.getY() <= 3672) || (tile.getX() >= 2756 && tile.getX() <= 2875 && tile.getY() >= 5512 && tile.getY() <= 5627) || (tile.getX() >= 3158 && tile.getX() <= 3181 && tile.getY() >= 3679 && tile.getY() <= 3697) || (tile.getX() >= 3280 && tile.getX() <= 3183 && tile.getY() >= 3883 && tile.getY() <= 3888);
	}
	
	public boolean isAtWildSafe() {
		return  (player.getX() >= 2940 && player.getX() <= 3395 && player.getY() <= 3524) ;
	}
	
	public int getWildLevel() {
		return (player.getY() - 3520)/8+1;
	}

}
